package com.shbaiche.bifenba.ui.match;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.Passport;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteGroupMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.ui.base.XStateFragmentAdapter;
import com.shbaiche.bifenba.ui.data.DataContentFragment;
import com.shbaiche.bifenba.ui.data.DataIntegralFragment;
import com.shbaiche.bifenba.ui.data.DataPlayerFragment;
import com.shbaiche.bifenba.ui.data.DataScheduleFragment;
import com.shbaiche.bifenba.ui.data.DataTeamFragment;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.my.LoginActivity;
import com.shbaiche.bifenba.ui.my.MessageListActivity;
import com.shbaiche.bifenba.ui.my.MyCollectionActivity;
import com.shbaiche.bifenba.ui.my.MyCommentListActivity;
import com.shbaiche.bifenba.ui.my.MyFollowActivity;
import com.shbaiche.bifenba.ui.my.SettingsActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xstatecontroller.XStateController;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import okhttp3.Call;

/**
 * 比分首页
 */

public class MatchFragment extends BaseFragment {

    @BindView(R.id.category_strip)
    CategoryTabStrip tabs;

    @BindView(R.id.contentLayout)
    XStateController contentLayout;

    @BindView(R.id.view_pager_match)
    ViewPager pager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private XStateFragmentAdapter adapter;


    public static MatchFragment newInstance() {
        return new MatchFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        tabs.type = 2;
        contentLayout.loadingView(View.inflate(getActivity(), R.layout.view_loading, null));

        List<Fragment> fragments = getChildFragmentManager().getFragments();
        if (fragments != null && fragments.size() > 0) {
            fragmentList.clear();
            String[] tits = new String[fragments.size()];
            int i = 0;
            for (Fragment fragment : fragments) {
                if (fragment instanceof Fragment) {
                    fragmentList.add(fragment);
                }
                tits[i] = "";
                i++;
            }
            adapter = new XStateFragmentAdapter(getFragmentManager(), fragmentList, tits);

            pager.setAdapter(adapter);
            pager.setOffscreenPageLimit(fragmentList.size());

            tabs.setViewPager(pager);

            if(fragmentList.size() > 1){
                pager.setCurrentItem(1);
            }

        }

        loadGroup();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            //相当于Fragment的onResume，为true时，Fragment已经可见
            if(contentLayout!=null && !(adapter!=null && adapter.getCount()>0)){
                loadGroup();
            }
        } else {
            //相当于Fragment的onPause，为false时，Fragment不可见
        }
    }

    private void loadGroup(){
        if (contentLayout != null) {
            contentLayout.showLoading();
        }
        CompeteGroupMessage message = new CompeteGroupMessage();
        message.setPassport(App.getPassport());
        message.setType(CompeteGroupMessage.CompeteGroupType.SCORE);
        NetApi.dataGroup(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                contentLayout.showContent();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                contentLayout.showContent();
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                contentLayout.showContent();
                CompeteGroupMessage result =new Gson().fromJson(response.getMessage(),CompeteGroupMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null && result.getVoList()!=null){
                        Log.e("t","onResponse");
                        fragmentList.clear();
                        fragmentList = new ArrayList<Fragment>();
                        String[] tits = new String[result.getVoList().size()];
                        for (int i = 0;i< result.getVoList().size();i++){
                            tits[i] = result.getVoList().get(i).getName();
                            MatchListFragment fragment = new MatchListFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("data",result.getVoList().get(i));
                            fragment.setArguments(bundle);
                            fragmentList.add(fragment);
                        }

                        Log.e("t","size:"+fragmentList.size());


                        adapter = new XStateFragmentAdapter(getFragmentManager(), fragmentList, tits);



                        pager.setAdapter(adapter);
                        pager.setOffscreenPageLimit(fragmentList.size());


                        tabs.setViewPager(pager);

                        if(fragmentList.size() > 1){
                            pager.setCurrentItem(1);
                        }
                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                }
            }
        });
    }


    private class DoClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.my_layout_info:
                    LoginActivity.launch(context);
                    break;
                case R.id.my_btn_message:
                    MessageListActivity.launch(context);
                    break;
                case R.id.my_btn_follow:
                    MyFollowActivity.launch(context);
                    break;
                case R.id.my_btn_collection:
                    MyCollectionActivity.launch(context);
                    break;
                case R.id.my_btn_comment:
                    MyCommentListActivity.launch(context);
                    break;
                case R.id.my_btn_settings:
                    SettingsActivity.launch(context);
                    break;
            }
        }
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_match;
    }
}
